using Jupiter.Infrastructure.Attributes;
using Jupiter.Infrastructure.Cache.Interfaces;
using Jupiter.Infrastructure.Domain.SqlSugar;
using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.DataPermissions;
using Jupiter.Infrastructure.Responses;
using Microsoft.Extensions.Configuration;
using SOP.DataTransfer.AppService.Authorities.Roles.Requests;
using Wips.AppService.Authorities.Roles;
using Wips.AppService.Authorities.Roles.Responses;
using Wips.AppService.Authorities.Roles.ViewModels;
using Wips.AppService.CommonViewModels;
using Wips.Domain.Models.Authorities.Model;

namespace Wips.AppService.SqlSugar.Authorities.Roles;

/// <summary>
/// 角色服务接口实现类
/// </summary>
[ServiceComponent]
public class RoleService : BaseQueryAppService<Role>, IRoleService
{
    /// <summary>
    /// 读取分页列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<PageResult<GetRolePagesResponse>> GetPagesAsync(
        GetRolePagesRequest request)
    {
        var result = await QueryNoTracking()
            .HasWhere(request.Keyword, p => p.Name.Contains(request.Keyword!))
            .HasWhere(request.Filter.Status != null,
                p => request.Filter.Status!
                    .Select(e => (Status)Convert.ToInt32(e))
                    .Contains(p.Status))
            .Select(p => new GetRolePagesResponse
            {
                Id = p.Id!,
                Name = p.Name,
                Remarks = p.Remarks,
                Status = p.Status,
                Created = p.Created,
                Updated = p.Updated,
                RoleType = p.RoleType
            })
            .ToPageAsync(request);

        return result;
    }

    /// <summary>
    /// 读取信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<RoleViewModel> GetByIdAsync(string id)
    {
        var result = await QueryNoTracking()
            .Where(p => p.Id == id)
            .ToListAsync(p => new RoleViewModel()
            {
                Id = p.Id!,
                Name = p.Name,
                Remarks = p.Remarks,
                Status = p.Status,

            });

        return result.FirstOrDefault() ?? new RoleViewModel();
    }


    /// <summary>
    /// 读取选择框数据列表
    /// </summary>
    /// <returns></returns>
    public async Task<List<SelectViewModel>> GetSelectDataAsync()
    {
      
        var result = await QueryNoTracking()

            .ToListAsync();
        var resultList = result.Select(t1 => new SelectViewModel
        {
            Id = t1.Id!,
            Label = t1.Name,
            Value = t1.Id!,
            Key = t1.Id!,
            Disabled = t1.Status == Status.Disabled || t1.RoleType != EnumRoleType.Normal
        }).ToList();

        return resultList;
    }

    /// <summary>
    /// 根据职位Id读取角色Id列表
    /// </summary>
    /// <param name="positionId">职位Id</param>
    /// <returns></returns>
    public async Task<List<string>> GetIdsByPositionIdAsync(string positionId)
    {
        var result = await QueryNoTracking<PositionRole>()
            .Where(p => p.PositionId == positionId)
            .Select(p => p.RoleId)
            .ToListAsync();

        return result;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="dbContext"></param>
    /// <param name="securityContextAccessor"></param>
    /// <param name="configuration"></param>
    /// <param name="cacheManager"></param>
    /// <param name="dataPermission"></param>
    public RoleService(ISqlSugarClient dbContext, ISecurityContextAccessor securityContextAccessor,
        IConfiguration? configuration, ICacheManager cacheManager, IDataPermission dataPermission) : base(dbContext,
        securityContextAccessor, configuration, cacheManager, dataPermission)
    {
    }
}